//
//  LQStatusTableViewCell.swift
//  Weibo
//
//  Created by ios on 16/8/12.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import SnapKit
// 子控件之间的间距
let StatusTableViewCellMargin: CGFloat = 10

class LQStatusTableViewCell: UITableViewCell {
    
    //  toolbar底部约束记录
    var toolBarTopConstraint: Constraint?
    
//  处理微博数据的ViewModel
    
    var statusViewModel:LQStatusViewModel?{
        didSet{
           originView.statusViewModel = statusViewModel
           StatusToolBarView.statusViewModel = statusViewModel
            
            //  卸载上一次约束
            toolBarTopConstraint?.uninstall()
           //  判断是否有转发微博对象
            if statusViewModel?.status?.retweeted_status != nil {
            //显示发表微博视图
                RetweetView.hidden = false
                
                //更新约束
                StatusToolBarView.snp_updateConstraints(closure: { (make) -> Void in
                 self.toolBarTopConstraint =  make.top.equalTo(RetweetView.snp_bottom).constraint
                })
                 //  绑定转发微博数据
                RetweetView.statusViewModel = statusViewModel
            }else{
            //隐藏发表微博视图
                RetweetView.hidden = true
                
                  //更新约束
                StatusToolBarView.snp_updateConstraints(closure: { (make) -> Void in
                  self.toolBarTopConstraint =  make.top.equalTo(originView.snp_bottom).constraint
                })
            }
           
}
    }

 //MARK:  --懒加载控件
//原创微博视图
    private lazy var originView: LQStatusOriginView = {
        let view = LQStatusOriginView()
        return view
    }()
    
 //转发微博视图
    private lazy var RetweetView: LQStatusRetweetView = {
       let view = LQStatusRetweetView()
       return view
    }()
    
//toolBar
    private lazy var StatusToolBarView:LQStatusToolBar = LQStatusToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier:reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件 设置约束
    private func setupUI(){
    //  修改contentView的背景色
    contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
     contentView.addSubview(originView)
     contentView.addSubview(RetweetView)
     contentView.addSubview(StatusToolBarView)
    
        
        //  设置约束
        originView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)

        }
        
        RetweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originView.snp_bottom)
            make.leading.equalTo(originView)
            make.trailing.equalTo(originView)
        }
        
        StatusToolBarView.snp_makeConstraints { (make) -> Void in
          self.toolBarTopConstraint =  make.top.equalTo(RetweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //  非常关键的约束, contentView的底部约束 = toolbar底部约束 + 间距
        //  约束要建立完整
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(StatusToolBarView)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
    
       }
 }
}